﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.BLL;

public partial class ProjectManage_TotalProject_Add : System.Web.UI.Page
{
    TotalProjectBLL bll = new TotalProjectBLL();
    CommitmentBLL cbll = new CommitmentBLL();
    private string sid = String.Empty;
    private string tid = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            tid = this.Request.QueryString["t"].ToString().Trim();
            sid = this.Request.QueryString["id"].ToString().Trim();
        }
        catch
        {

        }
        if (!IsPostBack)
        {
            bind();
            if (tid == "1")
            {
                TotalProjectEntity tp = bll.GetModelEntity(int.Parse(sid));
                ddltasktype.SelectedValue = tp.tasktype;
                ddlstage.SelectedValue = tp.stage;
                txttaskno.Text = tp.taskno;
                txtprojectno.Text = tp.projectno;
                txtname.Text = tp.name;
                ddlcommpany.SelectedValue = tp.commpany;

                ChoiceName_1.Text = tp.leadersname;
                HChoiceID_1.Value = tp.leaders;
                HNewChoiceID_1.Value = tp.leaders;

                rblprojecttype.SelectedValue = tp.projecttype;
                rblprogresstype.SelectedValue = tp.progresstype;
                ChoiceName_2.Text = tp.personname1;
                HChoiceID_2.Value = tp.person1;
                HNewChoiceID_2.Value = tp.person1;

                ChoiceName_3.Text = tp.personname2;
                HChoiceID_3.Value = tp.person2;
                HNewChoiceID_3.Value = tp.person2;

                ChoiceName_4.Text = tp.personname3;
                HChoiceID_4.Value = tp.person3;
                HNewChoiceID_4.Value = tp.person3;

                ChoiceName_5.Text = tp.plannername;
                HChoiceID_5.Value = tp.planner;
                HNewChoiceID_5.Value = tp.planner;

                txttpdate.Text = tp.tpdate.ToString();

                ChoiceName_6.Text = tp.businessname;
                HChoiceID_6.Value = tp.business;
                HNewChoiceID_6.Value = tp.business;

                txtcomments1.Text = tp.comments1;
                txtcomments2.Text = tp.comments2;
                txtreviewedchange.Text = tp.reviewedchange;
                txtmeetingchange.Text = tp.meetingchange;
                txtremark.Text = tp.remark;
                txtcooperationunit.Text = tp.cooperationunit;
                txtcooperationperson.Text = tp.cooperationperson;
                List<CommitmentEntity> list = new List<CommitmentEntity>();
                list = cbll.GetModelListEntity("tp_id=" + sid);
                int i = 1;
                foreach (CommitmentEntity com in list)
                {
                    HtmlInputHidden htb = null;
                    HtmlSelect hs = (HtmlSelect)this.FindControl("sltprofessional" + i);
                    hs.Value = com.professional;
                    hs = (HtmlSelect)this.FindControl("sltdepartments" + i);
                    hs.Value = com.departments.ToString();
                    TextBox tb = (TextBox)this.FindControl("txtcompletiondate" + i);
                    tb.Text = com.completiondate.ToString();
                    tb = (TextBox)this.FindControl("txtdeliverydate" + i);
                    tb.Text = com.deliverydate.ToString();

                    htb = (HtmlInputHidden)this.FindControl("HChoiceID_" + i + "1");
                    htb.Value = com.reviewer.ToString();
                    htb = (HtmlInputHidden)this.FindControl("HNewChoiceID_" + i + "1");
                    htb.Value = com.reviewer.ToString();
                    tb = (TextBox)this.FindControl("ChoiceName_" + i + "1");
                    tb.Text = com.reviewername.ToString();

                    htb = (HtmlInputHidden)this.FindControl("HChoiceID_" + i + "2");
                    htb.Value = com.branchreviewer.ToString();
                    htb = (HtmlInputHidden)this.FindControl("HNewChoiceID_" + i + "2");
                    htb.Value = com.branchreviewer.ToString();
                    tb = (TextBox)this.FindControl("ChoiceName_" + i + "2");
                    tb.Text = com.branchreviewername.ToString();

                    CheckBox cb = (CheckBox)this.FindControl("chkqualityplan" + i);
                    cb.Text = com.qualityplan;
                    cb = (CheckBox)this.FindControl("chkconferencereview" + i);
                    cb.Text = com.conferencereview;
                    i++;
                }
            }
        }
    }

    private void bind()
    {
        ddltasktype.DataSource = bll.getBaseData("2011");
        ddltasktype.DataTextField = "name";
        ddltasktype.DataValueField = "code";
        ddltasktype.DataBind();
        ddlstage.DataSource = bll.getBaseData("2012");
        ddlstage.DataTextField = "name";
        ddlstage.DataValueField = "code";
        ddlstage.DataBind();
        ddlcommpany.DataSource = bll.getBaseData("2013");
        ddlcommpany.DataTextField = "name";
        ddlcommpany.DataValueField = "code";
        ddlcommpany.DataBind();
        for (int i = 1; i <= 6; i++)
        {
            HtmlSelect hs = (HtmlSelect)this.FindControl("sltprofessional" + i);
            hs.DataSource = bll.getBaseData("2010");
            hs.DataTextField = "name";
            hs.DataValueField = "code";
            hs.DataBind();
            hs.Items.Insert(0, "");
            hs.Items[0].Value = "";
            hs = (HtmlSelect)this.FindControl("sltdepartments" + i);
            hs.DataSource = bll.getDept();
            hs.DataTextField = "name";
            hs.DataValueField = "id";
            hs.DataBind();
            hs.Items.Insert(0, "");
            hs.Items[0].Value = "";
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TotalProject tp = new TotalProject();
        tp.tasktype = ddltasktype.SelectedValue;
        tp.stage = ddlstage.SelectedValue;
        tp.taskno = txttaskno.Text.Trim();
        tp.projectno = txtprojectno.Text.Trim();
        tp.name = txtname.Text.Trim();
        tp.commpany = ddlcommpany.SelectedValue;
        tp.leaders = HNewChoiceID_1.Value;
        tp.projecttype = rblprojecttype.SelectedValue;
        tp.progresstype = rblprogresstype.SelectedValue;
        tp.person1 = HNewChoiceID_2.Value;
        tp.person2 = HNewChoiceID_3.Value;
        tp.person3 = HNewChoiceID_4.Value;
        tp.planner = HNewChoiceID_5.Value;
        if (txttpdate.Text != "")
            tp.tpdate = DateTime.Parse(txttpdate.Text);
        else
            tp.tpdate = DateTime.Now;
        tp.business = HNewChoiceID_6.Value;
        tp.comments1 = txtcomments1.Text;
        tp.comments2 = txtcomments2.Text;
        tp.reviewedchange = txtreviewedchange.Text;
        tp.meetingchange = txtmeetingchange.Text;
        tp.remark = txtremark.Text;
        tp.cooperationunit = txtcooperationunit.Text;
        tp.cooperationperson = txtcooperationperson.Text;
        List<Commitment> list = new List<Commitment>();
        for (int i = 1; i <= 6; i++)
        {
            Commitment com = new Commitment();
            HtmlSelect hs = (HtmlSelect)this.FindControl("sltprofessional" + i);
            com.professional = hs.Value;
            if (hs.Value != "")
            {
                hs = (HtmlSelect)this.FindControl("sltdepartments" + i);
                com.departments = int.Parse(hs.Value);
                TextBox tb = (TextBox)this.FindControl("txtcompletiondate" + i);
                com.completiondate = DateTime.Parse(tb.Text);
                tb = (TextBox)this.FindControl("txtdeliverydate" + i);
                com.deliverydate = DateTime.Parse(tb.Text);
                HtmlInputHidden htb = (HtmlInputHidden)this.FindControl("HChoiceID_" + i + "1");
                com.reviewer = int.Parse(htb.Value);
                htb = (HtmlInputHidden)this.FindControl("HChoiceID_" + i + "2");
                com.branchreviewer = int.Parse(htb.Value);
                CheckBox cb = (CheckBox)this.FindControl("chkqualityplan" + i);
                com.qualityplan = cb.Text;
                cb = (CheckBox)this.FindControl("chkconferencereview" + i);
                com.conferencereview = cb.Text;
                list.Add(com);
            }
        }
        if (tid == "0")
        {
            if (bll.Add(tp, list) == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功！');parent.leftFrame.location='ProjectManage_Left.aspx?ss=" + tp.tp_id + "';</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败！');</script>");
            }
        }
        else
        {
            tp.tp_id = int.Parse(sid);
            if (bll.Update(tp, list) == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');parent.leftFrame.location='ProjectManage_Left.aspx?ss=" + tp.tp_id + "';</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
    protected void btnAdChild_Click(object sender, EventArgs e)
    {
        if (sid == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请先保存项目信息！');</script>");
        }
        else
        {
            Response.Redirect("SingleProject_Add.aspx?t=0&p=" + sid);
        }
    }
}
